/**
 * 
 */
package org.vidyakiran.app.server.domain.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;

import org.datanucleus.jpa.annotations.Extension;
import org.vidyakiran.app.server.domain.reference.InterestTag;

import com.google.appengine.api.datastore.Key;

/**
 * @author Harpreet
 *
 */
@Entity
public class VolunteeringActivity implements Serializable {

	private static final long serialVersionUID = 8114164131311311472L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Key id;
	
	@Basic
	private String name;
	
	@Basic
	private String upperCaseName;
	
	@Basic
	private Date startDate;
	
	@Basic
	private Date endDate;
	
	//TODO Need a better way to handle this
	@Basic
	private String timeIntervalPrefered;
	
	@Basic
	private Long noOfVolunteers;
	
	@Basic
	private Boolean popularFlag = Boolean.FALSE;
	
	@Basic
	@Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private Date creationDate;
	
	@Basic
	@Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private Date lastModifiedDate;

	@ManyToOne(fetch = FetchType.LAZY)
	private Organisation organisation;
	
	@OneToMany(fetch = FetchType.LAZY)
	private List<Key> userKeys = new ArrayList<Key>();
	
	@Transient
	private List<User> users = new ArrayList<User>();
	
	@OneToMany(fetch = FetchType.LAZY)
//	@Extension(vendorName="datanucleus", key="gae.parent-pk", value="true")
	private List<Key> interestKeys;
	
//	@Transient
//	private List<InterestTag> interests;
	
	/**
	 * This method updates the time stamps, creation date and lastmodified date. 
	 */
	@PrePersist
	@PreUpdate
	public void updatePreSave() {
		Date today = Calendar.getInstance().getTime();
		if(id == null) {
			creationDate = today;
		}
		lastModifiedDate = today;
		
		upperCaseName = (name != null) ? name.toUpperCase() : null;
	}
	
	/**
	 * @return the id
	 */
	public Key getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Key id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the timeIntervalPrefered
	 */
	public String getTimeIntervalPrefered() {
		return timeIntervalPrefered;
	}

	/**
	 * @param timeIntervalPrefered the timeIntervalPrefered to set
	 */
	public void setTimeIntervalPrefered(String timeIntervalPrefered) {
		this.timeIntervalPrefered = timeIntervalPrefered;
	}

	/**
	 * @return the noOfVolunteers
	 */
	public Long getNoOfVolunteers() {
		return noOfVolunteers;
	}

	/**
	 * @param noOfVolunteers the noOfVolunteers to set
	 */
	public void setNoOfVolunteers(Long noOfVolunteers) {
		this.noOfVolunteers = noOfVolunteers;
	}

	/**
	 * @return the popularFlag
	 */
	public Boolean getPopularFlag() {
		return popularFlag;
	}

	/**
	 * @param popularFlag the popularFlag to set
	 */
	public void setPopularFlag(Boolean popularFlag) {
		this.popularFlag = popularFlag;
	}

	/**
	 * @return the creationDate
	 */
	public Date getCreationDate() {
		return creationDate;
	}

	/**
	 * @param creationDate the creationDate to set
	 */
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	/**
	 * @return the lastModifiedDate
	 */
	public Date getLastModifiedDate() {
		return lastModifiedDate;
	}

	/**
	 * @param lastModifiedDate the lastModifiedDate to set
	 */
	public void setLastModifiedDate(Date lastModifiedDate) {
		this.lastModifiedDate = lastModifiedDate;
	}

	/**
	 * @return the organisation
	 */
	public Organisation getOrganisation() {
		return organisation;
	}

	/**
	 * @param organisation the organisation to set
	 */
	public void setOrganisation(Organisation organisation) {
		this.organisation = organisation;
	}

	/**
	 * @return the userKeys
	 */
	public List<Key> getUserKeys() {
		return userKeys;
	}

	/**
	 * @param userKeys the userKeys to set
	 */
	public void setUserKeys(List<Key> userKeys) {
		this.userKeys = userKeys;
	}

	/**
	 * @return the users
	 */
	public List<User> getUsers() {
		return users;
	}

	/**
	 * @param users the users to set
	 */
	public void setUsers(List<User> users) {
		this.users = users;
	}

	/**
	 * @return the interestKeys
	 */
	public List<Key> getInterestKeys() {
		return interestKeys;
	}

	/**
	 * @param interestKeys the interests to set
	 */
	public void setInterestKeys(List<Key> interestKeys) {
		this.interestKeys = interestKeys;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((creationDate == null) ? 0 : creationDate.hashCode());
		result = prime * result + ((endDate == null) ? 0 : endDate.hashCode());
		result = prime
				* result
				+ ((lastModifiedDate == null) ? 0 : lastModifiedDate.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((noOfVolunteers == null) ? 0 : noOfVolunteers.hashCode());
		result = prime * result
				+ ((popularFlag == null) ? 0 : popularFlag.hashCode());
		result = prime * result
				+ ((startDate == null) ? 0 : startDate.hashCode());
		result = prime
				* result
				+ ((timeIntervalPrefered == null) ? 0 : timeIntervalPrefered
						.hashCode());
		result = prime * result
				+ ((upperCaseName == null) ? 0 : upperCaseName.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		VolunteeringActivity other = (VolunteeringActivity) obj;
		if (creationDate == null) {
			if (other.creationDate != null)
				return false;
		} else if (!creationDate.equals(other.creationDate))
			return false;
		if (endDate == null) {
			if (other.endDate != null)
				return false;
		} else if (!endDate.equals(other.endDate))
			return false;
		if (lastModifiedDate == null) {
			if (other.lastModifiedDate != null)
				return false;
		} else if (!lastModifiedDate.equals(other.lastModifiedDate))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (noOfVolunteers == null) {
			if (other.noOfVolunteers != null)
				return false;
		} else if (!noOfVolunteers.equals(other.noOfVolunteers))
			return false;
		if (popularFlag == null) {
			if (other.popularFlag != null)
				return false;
		} else if (!popularFlag.equals(other.popularFlag))
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		if (timeIntervalPrefered == null) {
			if (other.timeIntervalPrefered != null)
				return false;
		} else if (!timeIntervalPrefered.equals(other.timeIntervalPrefered))
			return false;
		if (upperCaseName == null) {
			if (other.upperCaseName != null)
				return false;
		} else if (!upperCaseName.equals(other.upperCaseName))
			return false;
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "VolunteeringActivity ["
				+ (creationDate != null ? "creationDate=" + creationDate + ", "
						: "")
				+ (endDate != null ? "endDate=" + endDate + ", " : "")
				+ (id != null ? "id=" + id + ", " : "")
				+ (lastModifiedDate != null ? "lastModifiedDate="
						+ lastModifiedDate + ", " : "")
				+ (name != null ? "name=" + name + ", " : "")
				+ (noOfVolunteers != null ? "noOfVolunteers=" + noOfVolunteers
						+ ", " : "")
				+ (popularFlag != null ? "popularFlag=" + popularFlag + ", "
						: "")
				+ (startDate != null ? "startDate=" + startDate + ", " : "")
				+ (timeIntervalPrefered != null ? "timeIntervalPrefered="
						+ timeIntervalPrefered + ", " : "")
				+ (userKeys != null ? "userKeys=" + userKeys : "") + "]";
	}
	
	


}
